package com.qst.rongxiaoserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qst.rongxiaoserver.common.JwtUtil;
import com.qst.rongxiaoserver.mapper.AdminMapper;
import com.qst.rongxiaoserver.model.AdminModel;
import com.qst.rongxiaoserver.model.dto.AdminLoginDto;
import com.qst.rongxiaoserver.service.IAdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, AdminModel> implements IAdminService {

    @Autowired
    JwtUtil jwtUtil;

    @Override
    public AdminLoginDto login(String name, String password) {
        QueryWrapper<AdminModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("password", password);
        AdminModel adminModel = this.getOne(queryWrapper);

//        2. 生成JWT token
       String token =  jwtUtil.generateToken(name);

        if (adminModel == null) {
            return null;
        } else {
            AdminLoginDto adminLoginDto = new AdminLoginDto();
            adminLoginDto.setToken(token);
            BeanUtils.copyProperties(adminModel, adminLoginDto);
            return adminLoginDto;
        }
    }
}
